package com.lry.sso.server;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import cn.hutool.core.codec.Base64;
import com.lry.sso.auth.common.util.RsaKeyHelper;
import org.springframework.util.DigestUtils;

/**
 *
 *
 * @author 李瑞益
 * @date 2019/12/8
 */
public class JWTTset {

    public static void main(String[] args) throws Exception {
        System.out.println("前端加密后的密码:");
        byte[] publicKey = getPublicKey(getKey());
        String encod = encod("111111", publicKey);
        System.out.println("加密后的密码："+encod);
        String parse = parse(encod, getPrivateKey(getKey()));
        System.out.println("解密后的密码："+parse);

        System.out.println("后端加密后的密码：");
        System.out.println(DigestUtils.md5DigestAsHex("111111".getBytes()));


    }


    public static Map<String, byte[]> getKey() throws IOException, NoSuchAlgorithmException {
        return RsaKeyHelper.generateKey("xx1WET12^%3^(WE45");
    }

    public static byte[] getPrivateKey(Map<String, byte[]> keyMap) throws IOException, NoSuchAlgorithmException {
        System.out.println("用户私钥："+RsaKeyHelper.getPrivateKey(keyMap));
        System.out.println("用户公钥："+RsaKeyHelper.getPublicKey(keyMap));
        return RsaKeyHelper.getPrivateKey(keyMap);
    }
    /**
     * 获取公钥
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    public static byte[] getPublicKey(Map<String, byte[]> keyMap) throws IOException, NoSuchAlgorithmException {
        System.out.println("用户私钥："+RsaKeyHelper.getPrivateKey(keyMap));
        System.out.println("用户公钥："+RsaKeyHelper.getPublicKey(keyMap));
        return RsaKeyHelper.getPublicKey(keyMap);
    }

    public static String encod(String password, byte[] publicKey) throws Exception {
        return RsaKeyHelper.encrypt(password, Base64.encode(publicKey));
    }

    public static String parse(String password, byte[] priblicKey) throws Exception {
        return RsaKeyHelper.decrypt(password,priblicKey);
    }
}
